<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 登录账户设置 -->
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="form" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 登录账户设置 -->
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="`userInfo`" />
          </el-tab-pane>
          <!-- 登录账户设置 -->
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="`jobInfo`" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { userInfoById } from '@/api/user'
import { saveUserInfo } from '@/api/employees'
import userInfo from '@/views/employees/components/user-info'
import jobInfo from '@/views/employees/components/job-info'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data() {
    return {
      userId: this.$route.params.id, // 从地址栏上获取用户动态路由参数
      userInfo: { // 存放用户详情数据
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    // 保存用户基本信息
    async saveUser() {
      await this.$refs.form.validate()
      // 注意要对password单独设置，否则更新后的密码就是一串加密的字符
      await saveUserInfo({ ...this.userInfo, password: this.userInfo.password2 })
      this.$message.success('更新成功')
    },
    // 获取用户详情数据
    async getUserDetail() {
      this.userInfo = await userInfoById(this.userId)
      // console.log(res)
    }
  }
}
</script>

<style>

</style>
